package com.yyj.web.controller.manager;

import com.yyj.common.BaseResponse;
import com.yyj.constant.WebConstant;
import com.yyj.enums.PaymentStatusEnum;
import com.yyj.enums.PaymentTypeEnum;
import com.yyj.pojo.user.SessionInfo;
import com.yyj.service.PaymentService;
import com.yyj.service.WaterBillService;
import com.yyj.service.WeixinPayService;
import com.yyj.util.DateUtil;
import com.yyj.util.RspUtils;
import com.yyj.web.controller.BaseController;
import com.yyj.web.protocol.PageRequest;
import com.yyj.web.protocol.req.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api(value = "管理员管理生活缴费相关接口" , description="管理员管理生活缴费相关接口")
@RestController
public class MPaymentContoller extends BaseController {

    @Autowired
    PaymentService paymentService;

    @Autowired
    WeixinPayService weixinPayService;

    @Autowired
    WaterBillService waterBillService;


    @ApiOperation(value = "管理员查询当月费用记录数量")
    @RequestMapping(value = "/auth/managers/payments/count", method = RequestMethod.GET)
    public BaseResponse queryPaymentCount( PaymentCountReq paymentCountReq
    ){
        try{
            paymentCountReq.setFeeDate(StringUtils.isNotBlank(paymentCountReq.getFeeDate()) ? paymentCountReq.getFeeDate() : DateUtil.getNowShortDate().substring(0 , 6));
            return RspUtils.success(paymentService.findPaymentCount(paymentCountReq));
        }catch(Exception e){
            logger.error("查询业主当月费用异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "管理员查询业主当月费用记录列表")
    @RequestMapping(value = "/auth/managers/payments/list", method = RequestMethod.GET)
    public BaseResponse queryPaymentList( PaymentListReq paymentCountReq
    ){
        try{
            paymentCountReq.setFeeDate(StringUtils.isNotBlank(paymentCountReq.getFeeDate()) ? paymentCountReq.getFeeDate() : DateUtil.getNowShortDate().substring(0 , 6));
            if(PaymentTypeEnum.WATER_FEE.getCode().equals(paymentCountReq.getType()) &&
                    !PaymentStatusEnum.IS_PAY.getCode().equals(paymentCountReq.getStatus())
                    ){
                WaterBillQueryReq waterBillQueryReq = new WaterBillQueryReq();
                waterBillQueryReq.setFeeDate(paymentCountReq.getFeeDate());
                waterBillQueryReq.setStatus(paymentCountReq.getStatus());
                return RspUtils.success(waterBillService.queryUnpaidWaterBill(waterBillQueryReq));
            }else{
                return RspUtils.success(paymentService.findPaymentList(paymentCountReq));
            }
        }catch(Exception e){
            logger.error("管理员查询业主当月费用列表异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "管理员查询业主当月费用详情")
    @RequestMapping(value = "/auth/managers/payments/detail/{id}", method = RequestMethod.GET)
    public BaseResponse queryHistoryPaymentList(@PathVariable long id
    ){
        try{
            return RspUtils.success(paymentService.findPaymentDetail(id));
        }catch(Exception e){
            logger.error("管理员查询业主历史费用详情异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "管理员查询业主历史费用列表")
    @RequestMapping(value = "/auth/managers/payments/history/{roomNum}", method = RequestMethod.GET)
    public BaseResponse queryHistoryPaymentList(@PathVariable String roomNum  , String type ,  PageRequest pageRequest
    ){
        try{
            PaymentQueryReq paymentQueryReq = new PaymentQueryReq();
            paymentQueryReq.setType(type);
            paymentQueryReq.setCurrentPage(pageRequest.getCurrentPage());
            paymentQueryReq.setPageSize(pageRequest.getPageSize());
            paymentQueryReq.setRoomNum("ALL".equals(roomNum) ? "" : roomNum);
            return RspUtils.success(paymentService.findPaymentList(paymentQueryReq));
        }catch(Exception e){
            logger.error("管理员查询业主历史费用列表异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "管理员发送提示缴费通知")
    @RequestMapping(value = "/auth/managers/payments/notify", method = RequestMethod.POST)
    public BaseResponse notifyPayment(@RequestBody PaymentNotifyReq paymentNotifyReq
    ){
        try{
            paymentService.pushMsgToOwners(paymentNotifyReq);
            return RspUtils.success();
        }catch(Exception e){
            logger.error("管理员发送提示缴费通知异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "测试管理员发送提示缴费通知")
    @RequestMapping(value = "/auth/managers/payments/testNotify", method = RequestMethod.POST)
    public BaseResponse notifyPaymentTest(@RequestBody PaymentNotifyReq paymentNotifyReq
    ){
        try{
            return RspUtils.success();
        }catch(Exception e){
            logger.error("管理员发送提示缴费通知异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

}
